from flask import render_template, redirect, url_for, request
from flask.ext.login import login_user, logout_user, login_required, current_user
from . import bp
from .. import db
from ..models import User, Post, Comment
from .forms import SignupForm, LoginForm, SettingsForm


@bp.route('/signup', methods=['GET', 'POST'])
def signup():
    form = SignupForm()
    if form.validate_on_submit():
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=form.password.data)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('account.login'))
    return render_template('account/signup.html', form=form)

@bp.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user, form.remember_me.data)
            return redirect(url_for('main.index'))
    return render_template('account/login.html', form=form)

@bp.route('/logout', methods=['GET', 'POST'])
@login_required
def logout():
    logout_user()
    return redirect(url_for('main.index'))

@bp.route('/settings/<username>', methods=['GET', 'POST'])
@login_required
def settings(username):
    #profile_dict = ['username', 'email', 'location', 'website', 'bio', 'twitter', 'weibo', 'github', 'instagram']
    profile = User.query.filter_by(username=username).first()
    form = SettingsForm()
    if form.validate_on_submit():
        #for j in profile_dict:
        #    profile.j = form.j.data
        profile.email = form.email.data
        profile.location = form.location.data
        profile.website = form.website.data
        profile.bio = form.bio.data
        profile.twitter = form.twitter.data
        profile.weibo = form.weibo.data
        profile.github = form.github.data
        profile.instagram = form.instagram.data
        profile.stackoverflow = form.stackoverflow.data
        db.session.add(profile)
        db.session.commit()
        return redirect(url_for('main.index'))
    #for k in profile_dict:
    #    form.k.data = profile.k
    form.username.data = profile.username
    form.email.data = profile.email
    form.location.data = profile.location
    form.website.data = profile.website
    form.bio.data = profile.bio
    form.twitter.data = profile.twitter
    form.weibo.data = profile.weibo
    form.github.data = profile.github
    form.instagram.data = profile.instagram
    form.stackoverflow.data = profile.stackoverflow
    return render_template('account/settings.html', form=form)

@bp.route('/user/<username>', methods=['GET', 'POST'])
def user(username):
    user = User.query.filter_by(username=username).first()
    user_posts = Post.query.filter_by(username=username).order_by(Post.post_time.desc())
    user_replies = Comment.query.filter_by(username=username)\
                                          .order_by(Comment.comment_time.desc())
    return render_template('account/user.html',
                            user=user,
                            user_posts=user_posts,
                            user_replies=user_replies)
